package logic

import (
	"context"
	"errors"
	"short-vid-joy/models/mysql"

	"short-vid-joy/shortrpc/internal/svc"
	"short-vid-joy/shortrpc/shortrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DelCollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDelCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelCollectLogic {
	return &DelCollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DelCollectLogic) DelCollect(in *shortrpc.DelCollectRequest) (*shortrpc.DelCollectResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User

	if err := findUser.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	var collect mysql.Collect

	if err := collect.FindCollectById(mysql.DB, in.CollectId); err != nil {
		return nil, err
	}

	var video mysql.Video
	//视频收藏数-1
	if err := video.UpdateVideoByCollectNum(mysql.DB, in.VideoId); err != nil {
		return nil, err
	}

	if err := collect.DelCollect(mysql.DB, in.CollectId); err != nil {
		return nil, err
	}

	return &shortrpc.DelCollectResponse{}, nil
}
